﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS.CDBNames;
using WebDS;
using IP.Core.IPCommon;
using WebUS;

public partial class F200_Quan_ly_hoa_chat_kem_theo_tuong_ung_voi_thiet_bi : System.Web.UI.Page
{
    #region Members
    
    #endregion

    #region Data Structures

    #endregion

    #region Private Methods
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!this.IsPostBack)
            {

                load_cbo_thiet_bi();
                load_dsach_hoa_chat();
                load_cbo_hoa_chat_kem_theo();
            }

        }
        catch (Exception v_e)
        {
            this.Response.Write(v_e.ToString());
        }

    }
    public string get_mapping_ten_tai_lieu(decimal i_dc_id)
    {
        try
        {
            string v_str_tai_lieu = "";
            US_CM_DM_TU_DIEN v_us = new US_CM_DM_TU_DIEN(i_dc_id);
            v_str_tai_lieu = v_us.strTEN;
            return v_str_tai_lieu;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }

    }

    private void load_cbo_thiet_bi()
    {
        US_DM_THIET_BI v_us_thiet_bi = new US_DM_THIET_BI();
        DS_DM_THIET_BI v_ds_thiet_bi = new DS_DM_THIET_BI();
        v_us_thiet_bi.FillDataset(v_ds_thiet_bi);
        m_cbo_ten_thiet_bi.DataSource = v_ds_thiet_bi.DM_THIET_BI;
        m_cbo_ten_thiet_bi.DataTextField = DM_THIET_BI.TEN_MAY;
        m_cbo_ten_thiet_bi.DataValueField = DM_THIET_BI.ID;
        m_cbo_ten_thiet_bi.DataBind();
    }
    // load danh sach hoa chat
    private void load_dsach_hoa_chat()
    {
        try
        {
            var v_str_id_thiet_bi = m_cbo_ten_thiet_bi.SelectedValue;
            if (v_str_id_thiet_bi == null || v_str_id_thiet_bi.Trim() == String.Empty)
            {
                return;
            }
            US_CM_DM_TU_DIEN v_us_hoa_chat = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_hoa_chat = new DS_CM_DM_TU_DIEN();
            v_us_hoa_chat.fill_tu_dien_cung_loai_ds(MA_LOAI_TU_DIEN.HOA_CHAT, v_ds_hoa_chat);
            v_us_hoa_chat.FillDataset(v_ds_hoa_chat,
             " WHERE ID NOT IN (SELECT ID_KEM_THEO FROM DM_KEM_THEO WHERE ID_THIET_BI ="
             + CIPConvert.ToDecimal(v_str_id_thiet_bi) + ") AND ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.HOA_CHAT);
            m_lst_hoa_chat.DataSource = v_ds_hoa_chat.CM_DM_TU_DIEN;
            m_lst_hoa_chat.DataTextField = CM_DM_TU_DIEN.TEN;
            m_lst_hoa_chat.DataValueField = CM_DM_TU_DIEN.ID;
            m_lst_hoa_chat.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    // load danh sach hoa chat kem theo
    private void load_cbo_hoa_chat_kem_theo()
    {
        try
        {
            var v_str_id_thiet_bi = m_cbo_ten_thiet_bi.SelectedValue;
            if (v_str_id_thiet_bi == null || v_str_id_thiet_bi.Trim() == String.Empty)
            {
                return;
            }
            US_CM_DM_TU_DIEN v_us_hoa_chat_kem_theo = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_hoa_chat_kem_theo = new DS_CM_DM_TU_DIEN();
            // v_us_hoa_chat_kem_theo.fill_tu_dien_cung_loai_ds(MA_LOAI_TU_DIEN.HOA_CHAT, v_ds_hoa_chat_kem_theo);
            v_us_hoa_chat_kem_theo.FillDataset(v_ds_hoa_chat_kem_theo,
                " WHERE ID IN (SELECT ID_KEM_THEO FROM DM_KEM_THEO WHERE ID_THIET_BI ="
                + CIPConvert.ToDecimal(v_str_id_thiet_bi) + ") AND ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.HOA_CHAT);
            m_lst_hoa_chat_thiet_bi.DataSource = v_ds_hoa_chat_kem_theo.CM_DM_TU_DIEN;
            m_lst_hoa_chat_thiet_bi.DataTextField = CM_DM_TU_DIEN.TEN;
            m_lst_hoa_chat_thiet_bi.DataValueField = CM_DM_TU_DIEN.ID;
            m_lst_hoa_chat_thiet_bi.DataBind(); ;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }

    // ko dung
    //private void load_cbo_nhan_vien()
    //{
    //    try
    //    {
    //        US_V_DM_NHAN_VIEN v_us_nv = new US_V_DM_NHAN_VIEN();
    //        DS_V_DM_NHAN_VIEN v_ds_nv = new DS_V_DM_NHAN_VIEN();
    //        v_us_nv.FillDataset(v_ds_nv);
    //        m_cbo_ten_thiet_bi.DataSource = v_ds_nv.V_DM_NHAN_VIEN;
    //        m_cbo_ten_thiet_bi.DataTextField = V_DM_NHAN_VIEN.HO_VA_TEN;
    //        m_cbo_ten_thiet_bi.DataValueField = V_DM_NHAN_VIEN.ID;
    //        m_cbo_ten_thiet_bi.DataBind();
    //    }
    //    catch (Exception v_e)
    //    {
    //        throw v_e;
    //    }
    //}
    //private void load_cbo_tai_lieu()
    //{
    //    try
    //    {
    //        US_CM_DM_TU_DIEN v_us_tai_lieu = new US_CM_DM_TU_DIEN();
    //        DS_CM_DM_TU_DIEN v_ds_tai_lieu = new DS_CM_DM_TU_DIEN();
    //        v_us_tai_lieu.FillDataset(v_ds_tai_lieu, " WHERE ID NOT IN (SELECT ID_TAI_LIEU FROM DM_PHAN_QUYEN_TAI_LIEU WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.TAI_LIEU + " AND ID_USE =" + CIPConvert.ToDecimal(m_cbo_ten_thiet_bi.SelectedValue) + ")");
    //        m_lst_hoa_chat.DataSource = v_ds_tai_lieu.CM_DM_TU_DIEN;
    //        m_lst_hoa_chat.DataTextField = CM_DM_TU_DIEN.TEN;
    //        m_lst_hoa_chat.DataValueField = CM_DM_TU_DIEN.ID;
    //        m_lst_hoa_chat.DataBind();
    //    }
    //    catch (Exception v_e)
    //    {
    //        throw v_e;
    //    }
    //}
    //private void load_cbo_tai_lieu_user()
    //{
    //    try
    //    {
    //        US_CM_DM_TU_DIEN v_us_tai_lieu = new US_CM_DM_TU_DIEN();
    //        DS_CM_DM_TU_DIEN v_ds_tai_lieu = new DS_CM_DM_TU_DIEN();
    //        v_us_tai_lieu.FillDataset(v_ds_tai_lieu, " WHERE ID IN (SELECT ID_TAI_LIEU FROM DM_PHAN_QUYEN_TAI_LIEU WHERE ID_LOAI_TU_DIEN = " + (int)e_loai_tu_dien.TAI_LIEU + " AND ID_USE =" + CIPConvert.ToDecimal(m_cbo_ten_thiet_bi.SelectedValue) + ")");
    //        m_lst_hoa_chat_thiet_bi.DataSource = v_ds_tai_lieu.CM_DM_TU_DIEN;
    //        m_lst_hoa_chat_thiet_bi.DataTextField = CM_DM_TU_DIEN.TEN;
    //        m_lst_hoa_chat_thiet_bi.DataValueField = CM_DM_TU_DIEN.ID;
    //        m_lst_hoa_chat_thiet_bi.DataBind(); ;
    //    }
    //    catch (Exception v_e)
    //    {
    //        throw v_e;
    //    }
    //}
    private void update_quyen_chuc_nang()
    {
        try
        {
            m_lbl_mess.Text = "";
            string v_str_id_chuc_nangs = "";
            foreach (ListItem ltTemp in this.m_lst_hoa_chat_thiet_bi.Items)
            {

                v_str_id_chuc_nangs += ltTemp.Value + ",";
            }
            //US_DM_KEM_THEO v_us_kem_theo = new US_DM_KEM_THEO();
            //v_us_kem_theo.dc
            //v_us_kem_theo.Insert(
            //US_DM_PHAN_QUYEN_TAI_LIEU v_us_quyen_tai_lieu = new US_DM_PHAN_QUYEN_TAI_LIEU();
            //v_us_quyen_tai_lieu.update_quyen_tai_lieu(CIPConvert.ToDecimal(m_cbo_ten_thiet_bi.SelectedValue), v_str_id_chuc_nangs);
            //m_lbl_mess.Text = "Cập nhật thành công!";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }

    #endregion

    //
    //
    // events
    //
    //


    protected void m_cmd_right_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_hoa_chat.Items.Count > 0 && m_lst_hoa_chat.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_hoa_chat.SelectedItem;
                selectedItem.Selected = false;
                m_lst_hoa_chat_thiet_bi.Items.Add(selectedItem);
                m_lst_hoa_chat.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }

    protected void m_cmd_right_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_hoa_chat.Items)
            {
                this.m_lst_hoa_chat_thiet_bi.Items.Add(ltTemp);
            }
            this.m_lst_hoa_chat.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            while (m_lst_hoa_chat_thiet_bi.Items.Count > 0 && m_lst_hoa_chat_thiet_bi.SelectedItem != null)
            {
                ListItem selectedItem = m_lst_hoa_chat_thiet_bi.SelectedItem;
                selectedItem.Selected = false;
                m_lst_hoa_chat.Items.Add(selectedItem);
                m_lst_hoa_chat_thiet_bi.Items.Remove(selectedItem);
            }

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }

    }
    protected void m_cmd_left_all_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            foreach (ListItem ltTemp in this.m_lst_hoa_chat_thiet_bi.Items)
            {
                this.m_lst_hoa_chat.Items.Add(ltTemp);
            }
            this.m_lst_hoa_chat_thiet_bi.Items.Clear();


        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            update_quyen_chuc_nang();

        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_user_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            load_dsach_hoa_chat();
            load_cbo_hoa_chat_kem_theo();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}